package com.pengtu.web.project;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.project.ProgressReport;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.project.SignUpContract;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.project.ProjectManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "progressreport.action?projectId=${projectId}")
public class ProgressreportAction extends BaseActionSupport<ProgressReport> {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */
	//
	private static final long serialVersionUID = 1L;

	private ProjectManager projectManager;
	private SystemManager sysManager;

	private String id;
	private ProgressReport progressReport;
	private Page<ProgressReport> page = new Page<ProgressReport>(10);
	private String projectId;
	private Attachment attachmention;
	private SignUpContract construction;

	private List<File> upload;
	// Struts2 Intercepter 自动装配
	private List<String> uploadFileName;
	private List<String> uploadContentType;
	private String[] consign;

	@Override
	public ProgressReport getModel() {
		// TODO Auto-generated method stub
		return progressReport;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		queryFilter.addFilter("project.id", projectId);
		if (!page.isOrderBySetted()) {
			page.order(Page.DESC);
			page.orderBy("createDate");
		}
		page = projectManager.findPageProGre(page, queryFilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			if(StringUtils.isNotEmpty(progressReport.getSignId())){
				construction = projectManager.getSign("id",progressReport.getSignId());
			}
			return "update";
		}
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		// 如果是工程进度则只保存一项,否则遍历保存
		String projsign = Struts2Utils.getParameter("check");
		if ("0".equals(projsign)) {
			// 保存工程
			ProjectInfo project = projectManager.getProj("id", projectId);
			if (null != project)
				progressReport.setProject(project);
			projectManager.saveProGre(progressReport);
			this.saveupfile(progressReport);
		} else {
			for (String cnId : consign) {
				String consignId = cnId.substring(cnId.length() - 1);
				ProgressReport progress = new ProgressReport();

				progress.setFinishInvestmentPercent(new Double(Struts2Utils
						.getParameter("finishInvestmentPercent" + consignId)));
				progress.setSignId(Struts2Utils.getParameter("signId"
						+ consignId));
				progress.setFinishinvestmenttotal(new Double(Struts2Utils
						.getParameter("finishinvestmenttotal" + consignId)));
				progress.setFinishmileagetotal(new Double(Struts2Utils
						.getParameter("finishmileagetotal" + consignId)));
				progress.setScheduledescribe(Struts2Utils
						.getParameter("scheduledescribe" + consignId));
				progress.setProgressPercent(new Double(Struts2Utils
						.getParameter("progressPercent" + consignId)));
				progress.setRemark(Struts2Utils.getParameter("remark"
						+ consignId));
				// 公有属性
				progress.setReportedperson(Struts2Utils
						.getParameter("reportedperson"));
				progress.setReportedorg(Struts2Utils
						.getParameter("reportedorg"));
				progress.setReportedDate(DateUtils
						.convertStringToDate(Struts2Utils
								.getParameter("reportedDate")));
				ProjectInfo project = projectManager.getProj("id", projectId);
				if (null != project)
					progress.setProject(project);
				projectManager.saveProGre(progress);
				this.saveupfile(progress);
			}
		}
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"工程形象进度!");
		return "successMessage";
	}

	public void prepareSaveUpdate() {
		if (StringUtils.isEmpty(id)) {
			progressReport = new ProgressReport();
		} else {
			progressReport = projectManager.getByProGre("id", id);
		}
	}

	/**
	 * 
	 * saveUpdate: 适用:修改进度信息
	 * 
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String saveUpdate() {
		projectManager.saveProGre(progressReport);
		String rootPath = Struts2Utils.getAppRootPath();
		if (null != upload) {
			for (int i = 0; i < upload.size(); i++) {
				Attachment attachmention = new Attachment();
				progressReport.setSchedulefile(uploadFileName.get(i));
				attachmention.setUpfile(upload.get(i));
				attachmention.setUpfileFileName(uploadFileName.get(i));
				attachmention.setType(uploadContentType.get(i));
				attachmention.setTableId(progressReport.getId());
				attachmention.setTableField("Schedulefile");
				attachmention.setTableName("ProgressReport");
				attachmention.setFileTime(new Date());
				attachmention.setPartName(SpringSecurityUtils
						.getCurrentUserName());
				sysManager.uploadFile(rootPath, attachmention);
				projectManager.saveProGre(progressReport);
			}
		}
		addActionMessage("保存工程形象进度!");

		return "successMessage";
	}

	/**
	 * 保存进度图
	 */
	protected void saveupfile(ProgressReport progre) {
		String rootPath = Struts2Utils.getAppRootPath();
		if (null != upload) {
			for (int i = 0; i < upload.size(); i++) {
				Attachment attachmention = new Attachment();
				progressReport.setSchedulefile(uploadFileName.get(i));
				attachmention.setUpfile(upload.get(i));
				attachmention.setUpfileFileName(uploadFileName.get(i));
				attachmention.setType(uploadContentType.get(i));
				attachmention.setTableId(progressReport.getId());
				attachmention.setTableField("Schedulefile");
				attachmention.setTableName("ProgressReport");
				attachmention.setFileTime(new Date());
				attachmention.setPartName(SpringSecurityUtils
						.getCurrentUserName());
				sysManager.uploadFile(rootPath, attachmention);
				projectManager.saveProGre(progre);
			}
		}
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		projectManager.deleteProGre(id);
		addActionMessage("删除工程想象进度");
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isEmpty(id)) {
			progressReport = new ProgressReport();
		} else {
			progressReport = projectManager.getByProGre("id", id);
		}
	}

	public Page<ProgressReport> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getProjectId() {
		return projectId;
	}

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String[] getConsign() {
		return consign;
	}

	public void setConsign(String[] consign) {
		this.consign = consign;
	}

	public SignUpContract getConstruction() {
		return construction;
	}

	@Autowired
	public void setSysManager(SystemManager sysManager) {
		this.sysManager = sysManager;
	}

}

